package javaCode.chapter12.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 徐州城
 * @version 1.0
 */
public class ArraysSortCustom {
    public static void main(String[] args) {

        int[] arr = {1,-12,30,8};
//        bubble01(arr);

        bubble02(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i2 - i1; //i1 - i2
            }
        });

        System.out.println("定制排序");
        System.out.println(Arrays.toString(arr));
    }

    //冒泡排序 + 定制
    public static void bubble02(int[] arr, Comparator c){
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //数组排序由 c.compare(arr[j],arr[j+1]) 返回来决定
                if (c.compare(arr[j],arr[j+1]) > 0){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    //使用冒泡排序
    public static void bubble01(int[] arr){
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //从小到大
                if (arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}
